﻿using System;
using System.Collections.Generic;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class ExecutionCounterTests
    {
        private ExecutionCounter ec;
        private FeatureIdentificationKey fik;

        [TestInitialize]
        public void Setup()
        {
            var id = Guid.NewGuid();
            fik = new FeatureIdentificationKey { LicenseEntitlementId = id };
            ec = new ExecutionCounter(new List<ExecutionCount> { new ExecutionCount(fik, 1, new List<ClientSession>())}, new TimeSpan(0, 0, 0, 300));
        }

        [TestMethod]
        public void Should_increment_existing_value()
        {
            var result = ec.CheckAndIncrement(fik, 5, new LicenseRequestInformation());
            result.Item1.Should().Be(true);
            result.Item2.Should().Be(2);
            Assert.AreEqual(ec.GetExecutionCount(fik).Executions, 2);
        }

        [TestMethod]
        public void Should_not_increment_existing_value()
        {
            var result = ec.CheckAndIncrement(fik, 1, new LicenseRequestInformation());
            result.Item1.Should().Be(false);
            result.Item2.Should().Be(1);
            Assert.AreEqual(ec.GetExecutionCount(fik).Executions, 1);
        }

        [TestMethod]
        public void Should_create_fik_If_it_does_not_exist()
        {
            var result = ec.CheckAndIncrement(FeatureIdentificationKey.Null, 1, new LicenseRequestInformation());
            result.Item1.Should().Be(true);
            result.Item2.Should().Be(1);
            Assert.AreEqual(ec.GetExecutionCount(fik).Executions, 1);
        }
    }
}